package com.example.demo.service.dataSource1.impl;

import com.example.demo.dao.dataSource1.GeographyMapper;
import com.example.demo.dao.dataSource1.StoryContentMapper;
import com.example.demo.dao.dataSource1.StoryMapper;
import com.example.demo.dao.dataSource2.ESDictMapper;
import com.example.demo.entity.bean.dataSource1.StoryBean;
import com.example.demo.entity.dataSource1.StoryContentEntity;
import com.example.demo.entity.dataSource1.StoryEntity;
import com.example.demo.entity.dataSource2.ESDictEntity;
import com.example.demo.service.dataSource1.interfaces.IStoryService;
import com.example.demo.wrap.StoryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * Created by Wen Gao on 2018/7/17.
 */
@Service
@Transactional(transactionManager = "xatx", propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)
public class StoryService implements IStoryService {

    @Autowired
    private StoryWrapper storyWrapper;

    @Autowired
    private ESDictMapper esDictMapper;


    @Override
    public StoryBean getSimpleStoryBean(int storyId) {
        return new StoryBean().initialize(storyId);
    }

    @Override
    public StoryBean getFullStoryBean(int storyId) {
        return new StoryBean().initialize(storyId);
    }

    @Override
    public StoryBean saveStoryBean(StoryBean storyBean) {
        // bean实现
//        new StoryBean().saveStoryBean(storyBean);
//        return this.getFullStoryBean(storyBean.getStoryEntity().getId());

        // wrapper实现
        storyWrapper.insertFullStoryBean(storyBean);
        return storyBean;
    }

    @Override
    public StoryBean deleteStoryBean(StoryBean storyBean) {
        return null;
    }

    @Override
    public StoryBean updateStoryBean(StoryBean storyBean) {
        return null;
    }

    /**
     * 测试atomikos XA事务
     * */
    public void testTX() {
        //1 .更新story表
        StoryEntity storyEntity = storyWrapper.selectStoryEntity(260000);
        storyEntity.setType(storyEntity.getType()+1);
        storyWrapper.updateStoryEntity(storyEntity);


        //2. 对esdict做操作
        ESDictEntity esDictEntity = esDictMapper.selectByWord("gaowen").get(0);
        esDictEntity.setSource(new Date().toString());
        esDictMapper.update(esDictEntity);

        //3. 使用StoryBean更新storyEntity
        StoryBean storyBean = new StoryBean().initialize(260000);
        storyEntity.setType(20);
        storyBean.updateStoryEntity(storyEntity);

        int a = 0/0; //制造异常

    }



}
